package org.ims.inv.service.system.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.ims.inv.dao.system.DAOAppDictionary;
import org.ims.inv.model.system.ModelAppDictionary;
import org.ims.inv.service.system.ServiceAppDictionary;

import cn.trymore.core.exception.DAOException;
import cn.trymore.core.exception.ServiceException;
import cn.trymore.core.service.impl.ServiceGenericImpl;
import cn.trymore.core.util.UtilCollection;
import cn.trymore.core.util.UtilString;

/**
 * Service: 系统字典
 * 
 * @author Jeccy.Zhao
 *
 */
public class ServiceAppDictionaryImpl
extends ServiceGenericImpl<ModelAppDictionary> implements ServiceAppDictionary
{
	
	/**
	 * The application dictionary repository.
	 */
	private DAOAppDictionary daoAppDict;
	
	/**
	 * The sole constructor.
	 * 
	 * @param dao
	 */
	public ServiceAppDictionaryImpl(DAOAppDictionary dao)
	{
		super(dao);
		this.daoAppDict = dao;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.ims.inv.service.system.ServiceAppDictionary#getByKey(java.lang.String)
	 */
	@Override
	public ModelAppDictionary getByKey(String key)
			throws ServiceException
	{
		try
		{
			return this.daoAppDict.getByKey(key);
		} 
		catch (Exception e)
		{
			throw new ServiceException(e);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.ims.inv.service.system.ServiceAppDictionary#getByKeyAndName(java.lang.String, java.lang.String)
	 */
	@Override
	public ModelAppDictionary getByTypeAndKey (String type, 
			String key) throws ServiceException
	{
		ModelAppDictionary entity = new ModelAppDictionary();
		entity.setDictType(type);
		entity.setDictKey(key);
		
		List<ModelAppDictionary> result = this.getByCriterion(entity);
		if (UtilCollection.isNotEmpty(result))
		{
			return result.get(0);
		}
		
		return null;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.ims.inv.service.system.ServiceAppDictionary#getByType(java.lang.String)
	 */
	@Override
	public List<ModelAppDictionary> getByType(String type)
			throws ServiceException
	{
		try
		{
			return daoAppDict.getByType(type);
		} 
		catch (DAOException e) 
		{
			throw new ServiceException(e);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.ims.inv.service.system.ServiceAppDictionary#isDictItemUnique(java.lang.String, java.lang.String)
	 */
	@Override
	public boolean isDictItemExisted(String type, 
			String value) throws ServiceException
	{
		return isDictItemExisted(type, value, null);
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.ims.inv.service.system.ServiceAppDictionary#isDictItemExisted(java.lang.String, java.lang.String, java.lang.Integer)
	 */
	@Override
	public boolean isDictItemExisted(String type, 
			String value, String id) throws ServiceException
	{
		try
		{
			DetachedCriteria criteria = DetachedCriteria.forClass(ModelAppDictionary.class);
			criteria.add(Restrictions.eq("dictType", type));
			criteria.add(Restrictions.eq("dictValue", value));
			
			List<ModelAppDictionary> rows = this.daoAppDict.getListByCriteria(criteria);
			
			if (UtilCollection.isNotEmpty(rows))
			{
				if (UtilString.isNotEmpty(id) && Integer.valueOf(id) > -1)
				{
					for (ModelAppDictionary row : rows)
					{
						if (row.getId().equals(id))
						{
							return false;
						}
					}
				}
				
				return true;
			}
			
			return false;
		} 
		catch (DAOException e)
		{
			throw new ServiceException(e);
		}
	}
	
	/*
	 * 	(non-Javadoc)
	 * @see cn.trymore.core.service.impl.ServiceGenericImpl#getCriterias(cn.trymore.core.model.ModelBase)
	 */
	@Override
	protected DetachedCriteria getCriterias(ModelAppDictionary entity)
	{
		DetachedCriteria criteria = DetachedCriteria.forClass(ModelAppDictionary.class);
		
		if (entity != null) 
		{
			if (UtilString.isNotEmpty(entity.getDictType()))
			{
				criteria.add(Restrictions.eq("dictType", entity.getDictType()));
			}
			
			if (UtilString.isNotEmpty(entity.getDictValue()))
			{
				criteria.add(Restrictions.eq("dictValue", entity.getDictValue()));
			}
			
			if (UtilString.isNotEmpty(entity.getDictKey()))
			{
				criteria.add(Restrictions.eq("dictKey", entity.getDictKey()));
			}
		}
		
		criteria.addOrder(Order.desc("modifiedDate"));
		
		return criteria;
	}
	
	public void setDaoAppDict(DAOAppDictionary daoAppDict)
	{
		this.daoAppDict = daoAppDict;
	}

	public DAOAppDictionary getDaoAppDict()
	{
		return daoAppDict;
	}
	
}
